package com.tl.khadmin.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.tl.khadmin.bean.Freight;
import com.tl.khadmin.bean.FreightTemplate;
import com.tl.khadmin.bean.Warehouse;
import com.tl.khadmin.bean.dto.FreightListDto;
import com.tl.khadmin.page.Page;
import com.tl.khadmin.service.FreightIService; 
import com.tl.khadmin.util.CommonUtil;

/**
 * 运费功能模块控制器
 * @author 123
 */
@RequestMapping(value = "/freight")
@RestController
public class FreightController {
	
	@Resource
	private FreightIService freightService; 
	
	/**
	 * 加载所有运费模板
	 * @param session
	 * @param param
	 * @return
	 */
	@RequestMapping(value="/template/list") 
	public Map<String, List<FreightTemplate>> listFreightTemplate(HttpSession session, @RequestParam Map<String ,Object> param){
		return CommonUtil.returnResultCodeJson("list", freightService.listFreightTemplate(session, param));
	}
	
	/**
	 * 查询运费模板详情
	 * @param session
	 * @param param
	 * @return
	 */
	@RequestMapping(value="/template/{id}") 
	public Map<String, FreightTemplate> queryFreightTemplate(@PathVariable("id") Integer id){
		return CommonUtil.returnResultCodeJson("freightTemplate", freightService.queryFreightTemplate(id));
	}
	
	/** 
	 * 新增运费模板 FreightTemplate
	 * @param 
	 * @param br
	 * @return
	 */
	@RequestMapping(value = "/template/save")
	@ResponseBody
	public String saveFreightTemplate(HttpSession session, @Valid FreightTemplate freightTemplate, BindingResult br) {
		return freightService.saveFreightTemplate(session, freightTemplate);
	}
	
	@RequestMapping(value = "/template/update")
	@ResponseBody
	public String updateFreight( @Valid FreightTemplate freightTemplate, BindingResult br) {
		return freightService.updateFreightTemplate(freightTemplate);
	}
	
	/**
	 * @param param level：查询快递公司列表
	 * @param param FreightName：名称，模糊查询
	 * @param param status：1启用，2禁用
	 * @return
	 */
	@RequestMapping(value="/list") 
	public Map<String, List<Freight>> listFreight(HttpSession session, @RequestParam Map<String ,Object> param){
		return CommonUtil.returnResultCodeJson("freight", freightService.listFreight(session, param));
	}

	@RequestMapping(value = "/showbypage")
	public Map<String, Page<FreightListDto>> showProductBrandByPage(HttpSession session, @RequestParam Map<String, Object> params) { 
		Integer pageNum = Integer.parseInt(params.get("pageNum").toString());
		Integer pageSize = Integer.parseInt(params.get("pageSize").toString());
		return CommonUtil.returnResultCodeJson("result", freightService.showFreightByPage(session, pageNum, pageSize, params));
	}
	
	/** 
	 * @param session
	 * @param freight
	 * @param regionVals 选中区域id，逗号分隔
	 * @param br
	 * @return
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public String saveFreight(HttpSession session, @Valid Freight freight, String regionVals, BindingResult br) {
		return freightService.saveFreight(session, freight, regionVals);
	}
	
	@RequestMapping(value = "/update")
	@ResponseBody
	public String updateFreight( @Valid Freight freight, BindingResult br) {
		return freightService.updateFreight(freight);
	}
	
	@RequestMapping(value = "/updatestatus")
	@ResponseBody
	public String updateStatus(Freight freight) {
		return freightService.updateStatus(freight);
	}

	@RequestMapping(value = "/deletebyids")
	@ResponseBody
	public String deleteByIds(String ids) {
		if(StringUtils.isBlank(ids)){
			return CommonUtil.returnMapResultByRows(0, "","未勾选");
		}
		return freightService.deleteByIds(ids);
	}

	@RequestMapping(value = "/querybyid")
	@ResponseBody
	public Map<String, Object> queryFreightById(String freightId) { 
		return freightService.queryFreightById(freightId);
	}
 
}
